package org.durcframework.rms.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

// key/value -> srId/operateCodes
public class UserPermission extends HashMap<String, List<String>> {
	private static final long serialVersionUID = 1L;

	private static boolean isEmpty(Object str) {
		return (str == null || "".equals(str));
	}

	public List<String> getBySrId(String srId) {
		return this.get(srId);
	}

	/**
	 * 菜单srId是否具有某operateCode权限
	 * 
	 * @param srId
	 * @param operateCode
	 * @return true,有
	 */
	public boolean hasPermission(String srId, String operateCode) {
		if (isEmpty(srId) || isEmpty(operateCode)) {
			return false;
		}

		List<String> operateCodes = this.get(srId);

		return operateCodes == null ? false : operateCodes
				.contains(operateCode);
	}

	/**
	 * 添加菜单权限
	 * 
	 * @param srId
	 *            菜单ID
	 * @param operateCode
	 *            权限代码
	 */
	public void addPermission(String srId, String operateCode) {
		List<String> operateCodes = this.get(srId);
		if (operateCodes == null) {
			operateCodes = new ArrayList<String>();
			this.put(srId, operateCodes);
		}
		operateCodes.add(operateCode);
	}

}
